/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package test.dbTest;



import java.util.LinkedList;
import java.util.List;

import org.apache.jmeter.samplers.AbstractSampler;
import org.apache.jmeter.samplers.Entry;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.testbeans.TestBean;
import org.apache.oro.text.MalformedCachePatternException;
import org.apache.oro.text.PatternCacheLRU;
import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RegexpVerifySample extends AbstractSampler implements   TestBean {
    private static final Logger log = LoggerFactory.getLogger(RegexpVerifySample.class);

    private static final long serialVersionUID = 234L;

    private transient String regexp;

  
    private transient String shuru;
    public RegexpVerifySample() {
    }

    

	


	public String getRegexp() {
		return regexp;
	}






	public void setRegexp(String regexp) {
		this.regexp = regexp;
	}






	


	


	@Override
	public SampleResult sample(Entry e) {
		SampleResult res = new SampleResult();
		res.setSampleLabel(this.getName());
		res.sampleStart();
		//List<MatchResult> result = process(getQuery(), );
		StringBuilder resdate=new StringBuilder();
		
		res.setResponseData(getRegexp(),"utf-8");
		res.setSamplerData(getRegexp()+"\r\t\n");
		res.setDataType(SampleResult.TEXT);
		res.setResponseCodeOK();
		res.setSuccessful(true);
		return res;
	}
	
	
	
	public String getShuru() {
		return shuru;
	}






	public void setShuru(String shuru) {
		this.shuru = shuru;
	}






	/**
	 * 参数化正则提取，并将提出来的值放到variables中 正则提取参考类GegexExtractor
	 * 正则验证参考类（查看结果树中）RenderAsRegexp
	 */
	private List<MatchResult> process(String textToParse, String regexp) {

		Perl5Matcher matcher = new Perl5Matcher();
		PatternMatcherInput input = new PatternMatcherInput(textToParse);

		PatternCacheLRU pcLRU = new PatternCacheLRU();
		org.apache.oro.text.regex.Pattern pattern;
		try {
			pattern = pcLRU.getPattern(regexp, Perl5Compiler.READ_ONLY_MASK);
		} catch (MalformedCachePatternException e) {
			return null;
		}
		List<MatchResult> matches = new LinkedList<>();
		while (matcher.contains(input, pattern)) {
			matches.add(matcher.getMatch());
		}
		if (matches.size() > 0) {

			//return matches.get(0).group(1);
			return matches;
		} else {
			return null;

		}

	}

 
}
